// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Text;
using System.Text.RegularExpressions;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using Templating.Grammar.Antlr;
using static Templating.Grammar.Antlr.TemplatingParser;

namespace SnapObjects.Data
{
    internal partial class DataTemplateParser
        : TemplatingParserBaseVisitor<StringBuilder>
        , ITemplatingParserVisitor<StringBuilder>
    {
        private readonly StringBuilder _builder;
        private readonly DataTemplateParserContext _context;
        private readonly ITemplateExportProvider _dataProvider;
        private readonly Regex _exprRegex;
        private readonly ITemplateDataFormater _dataFormat;

        public DataTemplateParser(
            DataTemplateParserContext context, 
            ITemplateExportProvider dataProvider,
            ITemplateDataFormater dataFormat)
        {
            _dataFormat = dataFormat;
            _context = context;
            _dataProvider = dataProvider;
            _builder = new StringBuilder();
            _exprRegex = new Regex("@[^\\(]*\\((.*)\\)", RegexOptions.Compiled);
            _context.Model = _dataProvider.GetDefaultModel();
        }

        public override StringBuilder VisitPortal([NotNull] PortalContext context)
        {
            var expressions = context.part();

            foreach (var expression in expressions)
            {
                _ = this.Visit(expression);
            }

            return _builder;
        }

        public override StringBuilder VisitOther([NotNull] OtherContext context)
        {
            return _builder.Append(context.TOKEN_OTHER().GetText());
        }

        public override StringBuilder VisitTerminal(ITerminalNode node)
        {
            return _builder.Append(node.GetText());
        }
    }
}
